package edu.gatech.aristotle.random.inputGenerator;

import java.io.IOException;
import java.util.Random;

import net.util.Debug;
import net.util.File_Handler;

import edu.gatech.aristotle.random.util.CONSTANT_FILE_NAME;
import edu.gatech.aristotle.random.util.CONSTANT_PRINT_TOKEN_INPUT_SPACE;
import edu.gatech.aristotle.random.util.RandomnessSelector;

public class Print_Token_RandomInputGenerator implements CONSTANT_PRINT_TOKEN_INPUT_SPACE, CONSTANT_FILE_NAME {
	public String doBatchMultiLine_print_tokenInput(){
		StringBuffer batchInput	= new StringBuffer();
		
		for ( int i = 1; i < NUMBER_OF_TEST_CASE + 1; i ++ ){
			String testCaseFileName	= 	experimentDir +
										print_tokenPath + 
										testCaseNewComingDir + 
										i +
										"_" + 
										PRINT_TOKENS_MULTI_LINE_TEST_FILE_NAME + 										
										TEST_CASE_FILE_POSTFIX;
			
			String singleInput	= this.doMultipleLine_print_tokensInput( testCaseFileName, 
																		INPUT_LINE_TO_PRINT_TOKENS );			
			//batchInput.append( singleInput + "\n" );
		}
		
		return batchInput.toString();
	}
	
	public String doBatchDefaultLengthScope_print_tokensInput(){
		StringBuffer batchInput	= new StringBuffer();
		
		for ( int i = 1; i < NUMBER_OF_TEST_CASE + 1; i ++ ){
			String testCaseFileName	= CONSTANT_FILE_NAME.experimentDir +
										CONSTANT_FILE_NAME.print_tokenPath + 
										CONSTANT_FILE_NAME.testCaseNewComingDir + 
										PRINT_TOKENS_SINGLE_LINE_TEST_FILE_NAME + 
										"_" + 
										i +
										TEST_CASE_FILE_POSTFIX;
			
			String singleInput	= doDefaultLengthScope_print_tokensInput( testCaseFileName );			
			batchInput.append( singleInput + "\n" );
		}
		
		return batchInput.toString();
	}
	
	public String doDefaultLengthScope_print_tokensInput( String testCaseFileName ){
		String input	= this.do_print_tokensInput( INPUT_LENGTH_TO_PRINT_TOKENS );
		
		try {
			File_Handler.write_to_file( testCaseFileName, input );
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return input;
	}
	
	public String doMultipleLine_print_tokensInput( String testCaseFileName, int lineNumberScope ){
		StringBuffer multiLineInput = new StringBuffer();
				
		int lineNumber = new Random().nextInt( lineNumberScope );
		
		for ( int i = 0; i < lineNumber; i++ ){
			String singleLineInput	= this.do_print_tokensInput( INPUT_LENGTH_TO_PRINT_TOKENS );
			
			multiLineInput.append( this.generateSpeicalStringHead() );			
			multiLineInput.append( singleLineInput + "\n" );
		}
		
		try {
			File_Handler.write_to_file( testCaseFileName, multiLineInput.toString() );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return multiLineInput.toString();		
	}
	
	private String generateSpeicalStringHead(){
		StringBuffer specialStringHead	= new StringBuffer();
		
		
		int specialStringsNumber	= new Random().nextInt(3); 
		for ( int i = 0; i < specialStringsNumber; i++ ){
			String speicalString	= print_token_specialStrings[ new Random().nextInt( print_token_specialStrings.length ) ];

			specialStringHead.append( speicalString );
		}
		
		int ifWithShart = new Random().nextInt( 2 );
		if ( ifWithShart == 1 )
			specialStringHead.append( "#" );
		
		return specialStringHead.toString();
	}

	/*
	 * Generate a random input of Siemens.print_token.exe
	 */
	public String do_print_tokensInput( int randomLengthOfInputScope ){
		int randomLength	= new Random().nextInt(randomLengthOfInputScope);
		
		StringBuffer input	= new StringBuffer();
		//Debug.println( "Find random indices for length: " + randomLength  + " from " + PRINT_TOKENS.length );
		
		int[] randomIndices	= RandomnessSelector.selectRandomIndex( PRINT_TOKENS.length - 1,
																	randomLength );
																	
		//Debug.println( "Find random indices: " );
		//Debug.printIntArray( randomIndices );
		
		for ( int i = 0; i < randomIndices.length; i++ ){
			input.append( PRINT_TOKENS[ randomIndices[i] ] );
		}
		
		return input.toString();
	}
}
